package com.controller;

import com.entity.Login;
import com.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    public ModelAndView login(HttpSession session, Login login,ModelAndView modelAndView){
        Login loginInfo = loginService.login(login.getLoginName(), login.getPassword());
        if(loginInfo!=null) session.setAttribute("loginInfo",loginInfo);
        modelAndView.setViewName(loginInfo!=null?"redirect:/buyer/index":"redirect:/login.jsp");
        return modelAndView;

    }

    @GetMapping("/register")
    public String register(){
        return "/buyer/register";
    }

    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/login.jsp";
    }
}
